﻿using ConfExpert.Datasets.MyDataTableAdapters;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Sistem
{
    public partial class Seri : XForm
    {
        public Seri()
        {
            InitializeComponent();
        }

        private void A10_Series_Load(object sender, EventArgs e)
        {
            try
            {
                ds = new Datasets.MyData();
                SeriTableAdapter seriTA = new SeriTableAdapter();
                SeriLinesTableAdapter seriLinesTA = new SeriLinesTableAdapter();
                seriTA.Fill(ds.Seri);
                seriLinesTA.Fill(ds.SeriLines);

                treeList1.OptionsView.ShowHorzLines = false;
                treeList1.OptionsView.ShowIndicator = false;
                treeList1.OptionsView.ShowVertLines = false;
                //treeList1.OptionsView.ShowColumns = false;

                treeList1.ParentFieldName = "ParentNode";
                treeList1.KeyFieldName = "SeriId";
                //treeList1.Columns["EntityName"].OptionsColumn.AllowEdit = false;
                //treeList1.Columns["TableName"].OptionsColumn.AllowFocus = false;
                //treeList1.OptionsSelection.EnableAppearanceFocusedCell = false;
                //treeList1.OptionsSelection.EnableAppearanceFocusedRow = false;

                //treeList1.Appearance.FocusedRow.Options.UseFont = true;
                BindingSource bs = new BindingSource(ds, "Seri");
                treeList1.DataSource = bs;
                treeList1.PopulateColumns();
                treeList1.Columns["TS"].Visible = treeList1.Columns["Deleted"].Visible = treeList1.Columns["ModifiedOn"].Visible = false;

                InitGridView(gridView1, new BindingSource(bs, "FK_SeriLines_Seri_SeriId"));
                gridView1.Columns["SeriId"].Visible = false;
                gridView1.Columns["SeriLinesId"].Visible = false;
                gridView2.OptionsView.ShowGroupPanel = false;
                gridView2.OptionsSelection.MultiSelect = true;
                gridControl2.UseEmbeddedNavigator = true;
                gridControl2.EmbeddedNavigator.ButtonClick += new NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);

                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                bsSeri = new BindingSource(ds, "xSeri");
                bsSeri.ListChanged += new System.ComponentModel.ListChangedEventHandler(bsSeri_ListChanged);
                gridControl2.DataSource = bsSeri;
                gridView2.Columns["idx"].Visible = false;
                gridView2.Columns["DepoId"].ColumnEdit = RI(Rp.Depo);
                gridView2.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);

                gleDepo.Properties.Assign(RI(Rp.Depo));
            }
            catch (Exception Ex)
            {
                throw Ex;
            }

        }

        void bsSeri_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
        {
            gridView2.BestFitColumns();
        }

        BindingSource bsSeri;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (gleDepo.EditValue == null)
                XtraMessageBox.Show("Bir depo seçin.");
            else
                try
                {
                    GetDetails("xSeri", "UpdSeries", ds.SeriLines.Serialize(), false, gleDepo.EditValue);
                    btnKaydet.Enabled = (ds.xSeri.Count > 0);
                }
                catch (Exception Ex)
                {
                    ds.RejectChanges();
                    Notify(string.Format(Ex.ToString()));
                }
        }

        void EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                if (MessageBox.Show("Delete the current row?", "Confirm deletion",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.Yes)
                    e.Handled = true;
            }
        }


        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            try
            {
                ExecuteNonQuery("UpdSeries", ds.xSeri.Serialize(), true, null);
                ds.AcceptChanges();

                //refreshing shelf table
                ShelfTableAdapter shelfTA = new ShelfTableAdapter();
                shelfTA.ClearBeforeFill = true;
                shelfTA.Fill(AppSession.Lookups.Shelf);
            }
            catch (Exception Ex)
            {
                ds.RejectChanges();
                throw Ex;
            }
            finally
            {
                this.btnKaydet.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

    }
}